package com.lei.baweiliao.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.lei.baweiliao.R;
import com.lei.baweiliao.bean.UserBean;
import com.lei.baweiliao.threadhelper.ThreadHelper;
import com.lei.baweiliao.utils.GreenDaoUtils;

public class AddTongxunActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_add_find;
    private EditText et_add_name;
    private ImageView iv_add_photo;
    private TextView tv_add_name;
    private Button bt_add_add;
    private RelativeLayout rl_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_tongxun);
        initView();
    }

    private void initView() {
        tv_add_find = (TextView) findViewById(R.id.tv_add_find);
        et_add_name = (EditText) findViewById(R.id.et_add_name);
        iv_add_photo = (ImageView) findViewById(R.id.iv_add_photo);
        tv_add_name = (TextView) findViewById(R.id.tv_add_name);
        bt_add_add = (Button) findViewById(R.id.bt_add_add);
        rl_add = (RelativeLayout) findViewById(R.id.rl_add);

        bt_add_add.setOnClickListener(this);
        tv_add_find.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_add_add:
                add();
                break;
            case R.id.tv_add_find:
                submit();
                break;
        }
    }

    private void add() {
        ThreadHelper.getInstance().getExecutors().execute(() -> {
            //参数为要添加的好友的username和添加理由
            try {
                EMClient.getInstance().contactManager().addContact(tv_add_name.getText().toString().trim(), "我邀请你成为我的好友");
//                UserBean userBean = new UserBean(, "");
//                GreenDaoUtils.getInstance(this).add();
                runOnUiThread(() -> Toast.makeText(AddTongxunActivity.this, "发送好友邀请成功", Toast.LENGTH_SHORT).show());
            } catch (HyphenateException e) {
                e.printStackTrace();
                runOnUiThread(() -> Toast.makeText(AddTongxunActivity.this, "发送好友邀请失败"+e.getDescription(), Toast.LENGTH_SHORT).show());
            }
        });
    }

    private void submit() {
        // validate
        String name = et_add_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "name不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        rl_add.setVisibility(View.VISIBLE);
        tv_add_name.setText(name);
        // TODO validate success, do something


    }
}
